import 'package:flutter/material.dart';
import "package:get/get.dart";
import 'package:pull_to_refresh/pull_to_refresh.dart';

import '../../../common/my_icon.dart';
import '../../../components/appbar_widget/appbar_widget.dart';
import '../../../components/status/empty.dart';
import '../../../components/status/load_more_state.dart';
import '../../../components/status/not_network.dart';
import '../../../config/alarm_type_config.dart';
import '../../../config/global_config.dart';
import '../../../controller/network/network_ctrl.dart';
import '../../../utils/screenutil/fit.dart';
import '../../../utils/tools/Tools.dart';
import 'ctrl/history_alarm_ctrl.dart';

class HistoryAlarm extends StatelessWidget {
  // 导入控制器
  final haCtrl = Get.put(HistoryAlarmCtrl());
  final _nc = Get.find<NetworkCtrl>();

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBarWidget('历史告警记录').build(context),
      body: Obx(
        () => Container(
          height: double.infinity,
          child: Stack(
            children: [
              Container(
                margin: EdgeInsets.only(top: 80.f + fit(_nc.topDistance.value)),
                child: haCtrl.MARKSYMBOL.value
                    ? SmartRefresher(
                        footer: CustomFooter(
                          builder: (BuildContext ct, LoadStatus mode) {
                            Widget body = LoadMoreState(mode);
                            return Container(child: Center(child: body));
                          },
                        ),
                        enablePullDown: false,
                        enablePullUp: true,
                        controller: haCtrl.refreshController.value,
                        onLoading: haCtrl.onLoading,
                        child: ListView.builder(
                          itemCount: haCtrl.listData.length,
                          itemBuilder: (BuildContext c, int i) => Container(
                            padding: EdgeInsets.only(
                              left: 20.f,
                              top: 15.f,
                              bottom: 15.f,
                            ),
                            decoration: BoxDecoration(
                              color: Colors.white,
                              border: haCtrl.listData.length - 1 == i
                                  ? null
                                  : Border(
                                      bottom: BorderSide(
                                        width: 1.f,
                                        color: GlobalConfig.borderColor,
                                      ),
                                    ),
                            ),
                            child: Row(
                              crossAxisAlignment: CrossAxisAlignment.center,
                              children: [
                                Container(
                                  width: 60.f,
                                  height: 60.f,
                                  margin: EdgeInsets.only(right: 10.f),
                                  decoration: BoxDecoration(
                                    color: Colors.red,
                                    borderRadius: BorderRadius.circular(60.f),
                                  ),
                                  child: Icon(
                                    MyIcon.gg,
                                    size: 34.f,
                                    color: Colors.white,
                                  ),
                                ),
                                Container(
                                  width: 320.f,
                                  margin: EdgeInsets.only(right: 10.f),
                                  child: Column(
                                    crossAxisAlignment:
                                        CrossAxisAlignment.start,
                                    children: [
                                      Text(
                                        "${AlarmTypeConfig.type[haCtrl.listData[i].type]}",
                                        style: TextStyle(
                                          color: GlobalConfig.titleColor,
                                          fontSize: 24.f,
                                        ),
                                        softWrap: true,
                                        textAlign: TextAlign.left,
                                        overflow: TextOverflow.ellipsis,
                                      ),
                                      SizedBox(height: 10.f),
                                      Text(
                                        "${Tools.dateFormat(DateTime.parse(haCtrl.listData[i].createTime), DataFormats.full)}",
                                        style: TextStyle(
                                          color: GlobalConfig.assistColor,
                                          fontSize: 20.f,
                                        ),
                                      ),
                                    ],
                                  ),
                                ),
                                Container(
                                  alignment: Alignment.topRight,
                                  width: 200.f,
                                  child: Column(
                                    crossAxisAlignment: CrossAxisAlignment.end,
                                    children: [
                                      Text(
                                        "${haCtrl.listData[i].status == 1 ? '已处理' : '未处理'}",
                                        style: TextStyle(
                                          color: haCtrl.listData[i].status == 1
                                              ? Colors.blue
                                              : Colors.red,
                                          fontSize: 24.f,
                                        ),
                                      ),
//                                      SizedBox(height: 15.f),
//                                      Text(
//                                        "${Tools.dateFormat(DateTime.parse(haCtrl.listData[i].createTime), DataFormats.mo_d_h_m)}",
//                                        style: TextStyle(
//                                          color: GlobalConfig.assistColor,
//                                          fontSize: 20.f,
//                                        ),
//                                      ),
                                    ],
                                  ),
                                ),
                              ],
                            ),
                          ),
                        ),
                      )
                    : Container(
                        width: 640.f,
                        height: double.infinity,
                        child: Center(
                          child: Empty(),
                        ),
                      ),
              ),

              // 选项卡、条件选择器
              Positioned(
                child: Container(
                  width: 640.f,
                  margin: EdgeInsets.only(top: fit(_nc.topDistance.value)),
                  decoration: BoxDecoration(boxShadow: GlobalConfig.boxShadow),
                  child: Container(
                    width: 640.f,
                    height: 80.f,
                    color: Colors.white,
                    child: Flex(
                      direction: Axis.horizontal,
                      children: [
                        Expanded(
                          flex: 1,
                          child: Row(
                            mainAxisAlignment: MainAxisAlignment.spaceBetween,
                            children: [
                              Container(
                                width: 280.f,
                                child: Column(
                                  mainAxisAlignment: MainAxisAlignment.center,
                                  children: [
                                    Text(
                                      "${haCtrl.communityName}",
                                      textAlign: TextAlign.left,
                                      maxLines: 1,
                                      overflow: TextOverflow.ellipsis,
                                    ),
                                    Text(
                                      "${haCtrl.liftLocation}号梯",
                                      style: TextStyle(
                                        fontSize: 20.f,
                                        color: GlobalConfig.assistColor,
                                      ),
                                      textAlign: TextAlign.left,
                                      maxLines: 1,
                                      overflow: TextOverflow.ellipsis,
                                    ),
                                  ],
                                ),
                              ),
                              SizedBox(
                                width: 1.f,
                                child: Container(
                                  height: 80.f,
                                  color: GlobalConfig.borderColor,
                                ),
                              ),
                              Container(
                                child: InkWell(
                                  onTap: () => haCtrl.getDateRange(context),
                                  child: Row(
                                    mainAxisAlignment:
                                        MainAxisAlignment.spaceBetween,
                                    children: [
                                      Container(
                                        width: 275.f,
                                        alignment: Alignment.centerLeft,
                                        padding: EdgeInsets.only(left: 15.f),
                                        child: Text(
                                          "${haCtrl.selectedDateRange.value}",
                                          softWrap: true,
                                          maxLines: 1,
                                          textAlign: TextAlign.left,
                                          overflow: TextOverflow.ellipsis,
                                          style: TextStyle(
                                            color: haCtrl.selectedDateRange
                                                        .value ==
                                                    "请选择日期"
                                                ? GlobalConfig.assistColor
                                                : Colors.black,
                                            fontSize: 22.f,
                                          ),
                                        ),
                                      ),
                                      InkWell(
                                        onTap: () => haCtrl.emptyDateRange(),
                                        child: Container(
                                          width: 64.f,
                                          height: 64.f,
                                          child: Icon(
                                            haCtrl.selectedDateRange.value ==
                                                    "请选择日期"
                                                ? Icons.chevron_right
                                                : MyIcon.cuowu,
                                            color: GlobalConfig.assistColor,
                                            size: 32.f,
                                          ),
                                        ),
                                      ),
                                    ],
                                  ),
                                ),
                              ),
                            ],
                          ),
                        ),
                      ],
                    ),
                  ),
                ),
              ),

              // 网络状态
              Positioned(top: 0, child: NotNetwork()),
            ],
          ),
        ),
      ),
    );
  }
}
